<template>
    <div>
      <!-- 面包屑导航区 -->
         <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片区 -->
        <el-card>
              <el-row :gutter="10">
                <el-col :span="6">
                     <el-input 
                     placeholder="请输入内容" 
                     class="input-with-select"
                      clearable
                      v-model="paramsInfo.query"
                      @clear="paramsInfo.query = ''"
                     >
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="6" >
                    <el-button type="primary" size="medium" @click="addShop">添加商品</el-button>
                </el-col>
              </el-row>

              <!-- 表格区域 -->
         <el-table :data="goodsList"  border stripe>
            <el-table-column type="index" label="#"   width="80"></el-table-column>
            <el-table-column prop="goods_name" label="商品名称"></el-table-column>
            <el-table-column prop="goods_price" label="商品价格(元)" width="180"></el-table-column>
            <el-table-column prop="goods_weight" label="商品重量" width="180"></el-table-column>
            <el-table-column prop="upd_time" label="创建时间" width="180">
            </el-table-column>
            <el-table-column label="操作" width="180">
                 <template  slot-scope="{row}">
                    <el-button type="primary" size="small" icon="el-icon-edit">编辑</el-button>
                    <el-button type="danger" size="small" icon="el-icon-delete" @click="delBtn(row.goods_id)">删除</el-button>
                </template>
            </el-table-column>
         </el-table>
           <!-- 分页 -->
           <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="paramsInfo.pagenum"
                :page-sizes="[1, 3, 5, 10]"
                :page-size="paramsInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
         </el-pagination>
            </el-card>
    </div>
</template>

<script>
export default {
  name:'listIndex',
  components: {},
  data() {
    return {
        paramsInfo:{
            query:'',
            pagenum:1,
            pagesize:10,
        },
        total:0,
        // 商品列表数据
        goodsList:[]
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
   async getGoodsList(){
        const {data:res} =  await this.$http.get('/goods',{
            params:this.paramsInfo
        })
        if(res.meta.status !== 200) return this.$message.error('获取数据失败')
        this.goodsList = res.data.goods
        this.total = res.data.total
    },
    handleSizeChange(newVal){
         this.paramsInfo.pagesize = newVal
         this.getGoodsList()
    },
    handleCurrentChange(newVal){
         this.paramsInfo.pagenum = newVal
         this.getGoodsList()
    },
    // 删除
     delBtn(id){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.$http.delete(`/goods/${id}`)
          this.$message({type: 'success',message: '删除成功!'})
           this.getGoodsList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
     },
     addShop(){
        this.$router.push('/goods/add')
     }
  }
};
</script>
<style scoped lang='less'>
     .el-breadcrumb{
    margin-bottom: 15px;
   }
   .el-table{
    margin-top: 15px;
    margin-bottom: 15px;
   }
</style>